﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL;

namespace BLL
{
  public class ProductBLL
  {
    public enum OrderBy
    { 
      CostASC = 1,
      CostDESC = 2,
      ViewDESC = 3,
      NewDESC = 4,
      KM = 5
    }
    public List<Product> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.OrderByDescending(p => p.CreatedDate).ToList();
      }
    }


    public List<Product> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isEnable == isEnable).OrderByDescending(p => p.CreatedDate).ToList();
      }
    }

    public List<Product> GetByTopNew(int top, bool isEnable, bool isNew)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Products.Where(p => p.isEnable == isEnable && p.isKM == isNew).Take(top).OrderByDescending(p => p.CreatedDate).ToList();
        }
    }

    public List<Product> GetByTopOther(int top, bool isEnable, int CateId, string productId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Products.Where(p => p.isEnable == isEnable && p.CategoryId == CateId && p.Id != productId).Take(top).OrderByDescending(p => p.CreatedDate).ToList();
        }
    }

    public List<Product> GetByTopBuy(int top, bool isEnable, bool isBuy)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Products.Where(p => p.isEnable == isEnable && p.isHot == isBuy).Take(top).OrderByDescending(p => p.CreatedDate).ToList();
        }
    }
    public List<Product> GetByPage(int page, int size,List<Product> list)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            int skip = (page - 1) * size;
            return list.Skip(skip).Take(size).ToList();
        }
    }
    public Product GetById(string id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.Id == id).FirstOrDefault();
      }
    }


    public Product GetById(string id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }
    public List<Product> GetByCateId(int cateId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.CategoryId == cateId).OrderByDescending(p => p.CreatedDate).ToList();
      }
    }
    public List<Product> GetByCateId(int cateId, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.CategoryId == cateId && p.isEnable == isEnable).OrderByDescending(p => p.CreatedDate).ToList();
      }
    }

    public List<Product> GetByCateId_ColorId(int cateId, int colorId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<ProductInColour> listProductInColor = data.ProductInColours.Where(p => p.IdColour == colorId).ToList();
            List<Product> listProduct = new List<Product>();
            foreach (var item in listProductInColor)
            {
                Product products = GetById(item.IdProduct);
                listProduct.Add(products);
            }
            return listProduct.Where(p=>p.CategoryId == cateId).ToList();
        }
    }

    public List<Product> GetByCateId_BrandId(int cateId, int brandId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Products.Where(p => p.CategoryId == cateId && p.Brand == brandId).ToList();
        }
    }
    public List<Product> GetByColorId_BrandId(int colorId, int brandId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<ProductInColour> listProductInColor = data.ProductInColours.Where(p => p.IdColour == colorId).ToList();
            List<Product> listProduct = new List<Product>();
            foreach (var item in listProductInColor)
            {
                Product products = GetById(item.IdProduct);
                listProduct.Add(products);
            }
            return listProduct.Where(p => p.Brand == brandId).ToList();
        }
    }
    public List<Product> GetByCateId_ColorId_BrandId(int cateId, int colorId, int brandId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<ProductInColour> listProductInColor = data.ProductInColours.Where(p => p.IdColour == colorId).ToList();
            List<Product> listProduct = new List<Product>();
            foreach (var item in listProductInColor)
            {
                Product products = GetById(item.IdProduct);
                listProduct.Add(products);
            }
            return listProduct.Where(p => p.CategoryId == cateId && p.Brand == brandId).ToList();
        }
    }

    public List<Product> GetByColorId(int colorId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<ProductInColour> listProductInColor = data.ProductInColours.Where(p => p.IdColour == colorId).ToList();
            List<Product> listProduct = new List<Product>();
            foreach (var item in listProductInColor)
            {
                Product products = GetById(item.IdProduct);
                listProduct.Add(products);
            }
            return listProduct.ToList();
        }
    }

    private bool CheckExits(string id, List<Product> lstProduct)
    {
        foreach (Product product in lstProduct)
        {
            if (product.Id == id)
                return true;
        }
        return false;
    }


    public List<Product> GetByFilter(List<int> cateId, List<int> colorId, List<int> brandId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<Product> listProduct = new List<Product>();

            if (cateId.Count() > 0 && colorId.Count() == 0 && brandId.Count() == 0)
            {
                //get category
                for (int i = 0; i < cateId.Count(); i++)
                {
                    List<Product> products = GetByCateId(cateId[i]);
                    if (products != null && listProduct.Count > 0)
                    {
                        foreach (var item in products)
                        {
                            if (!CheckExits(item.Id, listProduct))
                            {
                                listProduct.Add(item);
                            }
                        }
                        
                    }
                    else if (products != null && listProduct.Count == 0) listProduct.AddRange(products);
                }
            }
            else if (cateId.Count() == 0 && colorId.Count() > 0 && brandId.Count() == 0)
            {
                for (int i = 0; i < colorId.Count(); i++)
                {
                    List<Product> products = GetByColour(colorId[i]);
                    if (products != null && listProduct.Count > 0)
                    {
                        foreach (var item in products)
                        {
                            if (!CheckExits(item.Id, listProduct))
                            {
                                listProduct.Add(item);
                            }
                        }

                    }
                    else if (products != null && listProduct.Count == 0) listProduct.AddRange(products);
                }
            }

            else if (cateId.Count() == 0 && colorId.Count() == 0 && brandId.Count() > 0)
            {
                for (int i = 0; i < brandId.Count(); i++)
                {
                    List<Product> products = GetByBrand(brandId[i]);
                    if (products != null && listProduct.Count > 0)
                    {
                        foreach (var item in products)
                        {
                            if (!CheckExits(item.Id, listProduct))
                            {
                                listProduct.Add(item);
                            }
                        }

                    }
                    else if (products != null && listProduct.Count == 0) listProduct.AddRange(products);
                }
            }
            else if (cateId.Count() > 0 && colorId.Count() > 0 && brandId.Count() == 0)
            {
                for (int i = 0; i < cateId.Count(); i++)
                {
                    for (int j = 0; j < colorId.Count(); j++)
                    {
                        List<Product> products = GetByCateId_ColorId(cateId[i], colorId[j]);
                        if (products != null && listProduct.Count > 0)
                        {
                            foreach (var item in products)
                            {
                                if (!CheckExits(item.Id, listProduct))
                                {
                                    listProduct.Add(item);
                                }
                            }

                        }
                        else if (products != null && listProduct.Count == 0) listProduct.AddRange(products);
                    }

                }
            }

            else if (cateId.Count() == 0 && colorId.Count() > 0 && brandId.Count() > 0)
            {
                for (int i = 0; i < colorId.Count(); i++)
                {
                    for (int j = 0; j < brandId.Count(); j++)
                    {
                        List<Product> products = GetByColorId_BrandId(colorId[i], brandId[j]);
                        if (products != null && listProduct.Count > 0)
                        {
                            foreach (var item in products)
                            {
                                if (!CheckExits(item.Id, listProduct))
                                {
                                    listProduct.Add(item);
                                }
                            }

                        }
                        else if (products != null && listProduct.Count == 0) listProduct.AddRange(products);
                    }

                }
            }

            else if (cateId.Count() > 0 && colorId.Count() == 0 && brandId.Count() > 0)
            {
                for (int i = 0; i < cateId.Count(); i++)
                {
                    for (int j = 0; j < brandId.Count(); j++)
                    {
                        List<Product> products = GetByCateId_BrandId(cateId[i], brandId[j]);
                        if (products != null && listProduct.Count > 0)
                        {
                            foreach (var item in products)
                            {
                                if (!CheckExits(item.Id, listProduct))
                                {
                                    listProduct.Add(item);
                                }
                            }

                        }
                        else if (products != null && listProduct.Count == 0) listProduct.AddRange(products);
                    }

                }
            }
            else
            {
                for (int i = 0; i < cateId.Count(); i++)
                {
                    for (int k = 0; k < colorId.Count(); k++)
                    {
                        for (int j = 0; j < brandId.Count(); j++)
                        {
                            List<Product> products = GetByCateId_ColorId_BrandId(cateId[i], colorId[k], brandId[j]);
                            if (products != null && listProduct.Count > 0)
                            {
                                foreach (var item in products)
                                {
                                    if (!CheckExits(item.Id, listProduct))
                                    {
                                        listProduct.Add(item);
                                    }
                                }

                            }
                            else if (products != null && listProduct.Count == 0) listProduct.AddRange(products);
                        }
                    }
                }
            }
            return listProduct;
        } 
    }

    

    public List<Product> GetByCateParentId(int cateId, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<ProdCate> list = data.ProdCates.Where(p => p.Parent == cateId && p.isEnable == true).ToList();
        List<Product> listProduct = new List<Product>();
        foreach (var item in list)
        {
          List<Product> products = GetByCateId(item.Id);
          listProduct.AddRange(products);
        }
        return listProduct.ToList();
      }
    }

    public List<Product> GetByCateParentId(int cateId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<ProdCate> list = data.ProdCates.Where(p => p.Parent == cateId && p.isEnable == true).ToList();
            List<Product> listProduct = new List<Product>();
            foreach (var item in list)
            {
                List<Product> products = GetByCateId(item.Id);
                listProduct.AddRange(products);
            }

            return listProduct;
        }
    }

    public List<Product> GetByTag(string tag,bool isEnable,int page,int size,out int countItem)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        int skip = (page - 1) * size;
        List<Product> list = data.Products.Where(p => p.Tag.Contains(tag) && p.isEnable == isEnable).OrderByDescending(p => p.CreatedDate).ToList();
        countItem = list.Count;
        return list.Skip(skip).Take(size).ToList(); ;
      }
    }
    public List<Product> GetByTag(string tag)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.Tag.Contains(tag) && p.isEnable == true).ToList();
      }
    }
    public List<Product> GetByCateId(int cateId, bool isEnable, int page,int size,out int countItem)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        int skip = (page - 1) * size;
        List<Product> list =  data.Products.Where(p => p.CategoryId == cateId && p.isEnable == isEnable).OrderByDescending(p => p.CreatedDate).ToList();
        countItem = list.Count;
        return list.Skip(skip).Take(size).ToList();
      }
    }
    public List<Product> GetByCateParentId(int cateId, bool isEnable, int page, int size, out int countItem)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<ProdCate> list = data.ProdCates.Where(p => p.Parent == cateId && p.isEnable == true).ToList();
        List<Product> listProduct = new List<Product>();
        foreach (var item in list)
        {
          List<Product> products = GetByCateId(item.Id);
          listProduct.AddRange(products);
        }
        int skip = (page - 1) * size;
        countItem = listProduct.Count;
        return listProduct.Skip(skip).Take(size).ToList();
      }
    }
    public List<Product> GetByHot(int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isEnable == true).OrderByDescending(p => p.CreatedDate).Skip(0).Take(top).ToList();
      }
    }
    public List<Product> GetByHot(int cateId, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.CategoryId == cateId  && p.isEnable == true).OrderByDescending(p => p.CreatedDate).Skip(0).Take(top).ToList();
      }
    }
    
    public List<Product> GetByView()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isEnable == true).OrderByDescending(p => p.View).ToList();
      }
    }
    public List<Product> GetByView(int categoryId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isEnable == true && p.CategoryId == categoryId).OrderByDescending(p => p.View).ToList();
      }
    }
    public List<Product> GetByVote(int categoryId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isEnable == true && p.CategoryId == categoryId).OrderByDescending(p => p.Vote).ToList();
      }
    }
    public List<Product> GetByVote()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isEnable == true).OrderByDescending(p => p.Vote).ToList();
      }
    }
    public List<Product> GetByHot(bool isHot, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isHot == isHot && p.isEnable == true).OrderByDescending(p => p.View).Skip(0).Take(top).ToList();
      }
    }
    public List<Product> GetByHot(bool isHot)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isHot == isHot && p.isEnable == true).OrderByDescending(p => p.View).ToList();
      }
    }
    public List<Product> GetByKM(bool isKM, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.isKM == isKM && p.isEnable == true).OrderByDescending(p => p.View).Skip(0).Take(top).ToList();
      }
    }
    public DataTable GetByRandom(int cateId,int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        var result = data.GetRandomProduct(cateId).Take(top);
        DataTable table = new DataTable();
        table.Columns.Add("Id");
        table.Columns.Add("Name");
        table.Columns.Add("Name_En");
        table.Columns.Add("Image");
        table.Columns.Add("Cost");
        table.Columns.Add("Cost_Sale");

        
          foreach (var item in result)
          {
            DataRow row = table.NewRow();
            row["Id"] = item.Id;
            row["Name"] = item.Name;
            row["Name_En"] = item.Name_En;
            row["Image"] = item.Image_01;
            row["Cost"] = (item.Cost == null ? "0" : String.Format("{0:0,0}", item.Cost.Value) + " VND");
            if (item.Cost_Sale != null)
              row["Cost_Sale"] = (item.Cost_Sale == null ? "0" : String.Format("{0:0,0}", item.Cost_Sale.Value) + " VND");
            else
              row["Cost_Sale"] = "-1";

            table.Rows.Add(row);
          }        
        return table;
      }
    }
    public List<Product> GetByRandom(int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        var result = data.GetRandomAllProduct().Take(top);
        List<Product> list = new List<Product>();


        foreach (var item in result)
        {
          Product obj = GetById(item.Id);
          list.Add(obj);
        }
        return list;
      }
    }
    public List<Product> GetByStatus(int status)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.Status == status).ToList();
      }
    }
    public List<Product> GetBySize(int size)
    {
      ProductInSizeBLL pisData = new ProductInSizeBLL();
      List<Product> list = new List<Product>();
      var listSize = pisData.GetBySizeId(size);
      foreach (var item in listSize)
      {
        Product obj = GetById(item.IdProduct);
        if (obj != null)
          list.Add(obj);
      }
      return list;
    }
    public List<Product> GetByColour(int colour)
    {
      ProductInColourBLL pisData = new ProductInColourBLL();
      List<Product> list = new List<Product>();
      var listColour = pisData.GetByColourId(colour);
      foreach (var item in listColour)
      {
        Product obj = GetById(item.IdProduct);
        if (obj != null)
          list.Add(obj);
      }
      return list;
    }
    public List<Product> GetByBrand(int brand)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Products.Where(p => p.Brand == brand).ToList();
      }
    }  
    public List<Product> SearchByBrand(List<Product> list, int brand)
    {
      return list.Where(p => p.Brand == brand).ToList();
    }      
    public List<Product> SearchByCate(List<Product> list, int cate)
    {
       return list.Where(p => p.CategoryId == cate).ToList();
    } 
    public List<Product> SearchByColour(List<Product> list, int colourId)
    {
      ProductInColourBLL picData = new ProductInColourBLL();
      List<Product> listProduct = new List<Product>();
      List<ProductInColour> listProductInColour = picData.GetByColourId(colourId);
      foreach (var item in listProductInColour)
      {
        var product = list.Where(p => p.Id == item.IdProduct).FirstOrDefault();
        if (product != null)
          listProduct.Add(product);
      }
      return listProduct;
    }
    public List<Product> SearchBySize(List<Product> list, int sizeId)
    {
      ProductInSizeBLL pizData = new ProductInSizeBLL();
      List<Product> listProduct = new List<Product>();
      List<ProductInSize> listProductInSize = pizData.GetBySizeId(sizeId);
      foreach (var item in listProductInSize)
      {
        var product = list.Where(p => p.Id == item.IdProduct).FirstOrDefault();
        if (product != null)
          listProduct.Add(product);
      }
      return listProduct;
    }
    public List<Product> SearchOrderBy(List<Product> list, OrderBy order)
    {
      if (order == OrderBy.CostASC)
        return list.OrderBy(p => p.Cost).ToList();
      else if (order == OrderBy.CostDESC)
        return list.OrderByDescending(p => p.Cost).ToList();
      else if (order == OrderBy.ViewDESC)
        return list.OrderByDescending(p => p.View).ToList();
      else if (order == OrderBy.NewDESC)
        return list.OrderByDescending(p => p.CreatedDate).ToList();
      else
        return list.Where(p => p.isKM == true).ToList();
    }
    public List<Product> SearchById(List<Product> list, string id)
    {
         return list.Where(p => p.Id == id).ToList();
    }
    public List<Product> SearchByKey(List<Product> list, string key)
    {
        return list.Where(p => p.Tag.Contains(key)).ToList();
    }
    public List<Product> SearchByCostStart(List<Product> list, decimal cost_start)
    {
        return list.Where(p => p.Cost == null || (decimal)p.Cost >= cost_start ).ToList();
    }
    public List<Product> SearchByCostEnd(List<Product> list, decimal cost_end)
    {
         return list.Where(p => p.Cost == null || (decimal)p.Cost <= cost_end).ToList();
    }
    public string GetMaxId()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product obj = data.Products.OrderByDescending(p => p.Id).FirstOrDefault();
        return obj == null ? "1" : (obj.Id + 1).ToString();
      }
    }
    public bool IsCheckExitsId(string id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        var product = GetById(id);
        return product != null ? true : false;
      }
    }
    public void Enable(string id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product obj = data.Products.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }
    public void Enable(string id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product obj = data.Products.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }
    //Add View
    public void AddView(string id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product obj = data.Products.Where(p => p.Id == id).FirstOrDefault();
        obj.View = obj.View + 1;

        data.SubmitChanges();
      }
    }
    public void Add(Product obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Products.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }
    public void Update(Product obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product product = data.Products.Where(p => p.Id == obj.Id).FirstOrDefault();
        if (product != null)
        {
          product.Name = obj.Name;
          product.Name_En = obj.Name_En;
          product.Cost = obj.Cost;
          product.Cost_Sale = obj.Cost_Sale;
          product.Origin = obj.Origin;
          product.Status = obj.Status;
          product.Brand = obj.Brand;
          product.Content = obj.Content;
          product.Content_En = obj.Content_En;
          product.Quantity = obj.Quantity;
          product.CategoryId = obj.CategoryId;
          product.Vote = obj.Vote;
          product.TotalVote = obj.TotalVote;
          product.isHot = obj.isHot;
          product.isKM = obj.isKM;
          product.View = obj.View;
          product.Tag = obj.Tag;
          product.isEnable = obj.isEnable;
          product.CreatedDate = obj.CreatedDate;
          product.Image_01 = obj.Image_01;
          product.Image_02 = obj.Image_02;
          product.Image_03 = obj.Image_03;
          product.Image_04 = obj.Image_04;
          product.Image_05 = obj.Image_05;
          product.Image_06 = obj.Image_06;
          product.LinkGoc = obj.LinkGoc;
          product.UuDai = obj.UuDai;
          product.Label = obj.Label;
          product.Description = obj.Description;
          product.LabelId = obj.LabelId;

          data.SubmitChanges();
        }
      }
    }
    public void SubQuantity(string id, int quantity)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product obj = data.Products.Where(p => p.Id == id).FirstOrDefault();
        obj.Quantity -= quantity;

        data.SubmitChanges();
      }
    }
    public void AddQuantity(string id, int quantity)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product obj = data.Products.Where(p => p.Id == id).FirstOrDefault();
        obj.Quantity += quantity;

        data.SubmitChanges();
      }
    }
    public void Vote(string id, int vote)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product product = data.Products.Where(p => p.Id == id).FirstOrDefault();
        if (product != null)
        {
          product.Vote = (product.Vote * product.TotalVote + vote) / (product.TotalVote + 1);
          product.TotalVote++;
          data.SubmitChanges();
        }
      }
    }

    public void Delete(string id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Product product = data.Products.Where(p => p.Id == id).FirstOrDefault();
        if (product != null)
        {
          data.Products.DeleteOnSubmit(product);
          data.SubmitChanges();
        }
      }
    }
    public void DeleteByCateId(int cateId)    
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Product> list = data.Products.Where(p => p.CategoryId == cateId).ToList();
        if (list.Count > 0)
        {
          data.Products.DeleteAllOnSubmit(list);
          data.SubmitChanges();
        }
      }
    }
  }
}
